package com.hz.ruoyiquality.controller;


import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.ruoyiquality.domain.LgQuality;
import com.hz.ruoyiquality.mapper.LgQualityMapper;
import com.hz.ruoyiquality.service.LgQualityService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * @author gj
 * @version 1.0
 * @date 2025/3/3 14:04
 */
@RestController
@RequestMapping("/qualityMan")
public class LgQualityController extends BaseController {
    @Autowired
    private LgQualityService lgQualityService;
    /**
     * 质检员登录
     * @param account 账号
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LgQuality request) {
        String account = request.getAccount();
        String password = request.getPassword();
        LgQuality quality = lgQualityService.login(account, password);
        if (quality != null) {
            return AjaxResult.success("登录成功", quality);
        } else {
            return AjaxResult.error("账号或密码错误");
        }
    }

    /**
     * 获取质检员列表
     */
    @RequiresPermissions("quality:qualityMan:list")
    @GetMapping("/list")
    public TableDataInfo list(LgQuality lgQuality)
    {
        startPage();
        lgQuality.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<LgQuality> list = lgQualityService.selectLgQualityList(lgQuality);
        return getDataTable(list);
    }


    /**
     * 新增质检员
     */
    @RequiresPermissions("quality:qualityMan:add")
//    @Log(title = "质检员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LgQuality lgQuality)

    {
        lgQuality.setCreateBy(String.valueOf(BaseDomain.getUserId()));

        if (!lgQualityService.checkLgQualityNameUnique(lgQuality))
        {
            return error("新增质检员'" + lgQuality.getAccount() + "'失败，质检员账号已存在");
        }
        else if (!lgQualityService.checkLgQualityCodeUnique(lgQuality))
        {
            return error("新增质检员'" + lgQuality.getUsername() + "'失败，质检员用户名已存在");
        }
      // lgQuality.setCreateBy(SecurityUtils.getUsername());
        return toAjax(lgQualityService.insertLgQuality(lgQuality));
    }



    /**
     * 根据质检员编号获取详细信息
     */
    @RequiresPermissions("quality:qualityMan:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id)
    {
        return success(lgQualityService.selectLgQualityById(id));
    }

    /**
     * 修改质检员
     */
    @RequiresPermissions("quality:qualityMan:edit")
    @Log(title = "质检员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LgQuality lgQuality)
    {
        if (!lgQualityService.checkLgQualityNameUnique(lgQuality))
        {
            return error("修改质检员'" + lgQuality.getAccount() + "'失败，质检员账号已存在");
        }
        else if (!lgQualityService.checkLgQualityCodeUnique(lgQuality))
        {
            return error("修改质检员'" + lgQuality.getUsername() + "'失败，质检员用户名已存在");
        }
        lgQuality.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(lgQualityService.updateLgQuality(lgQuality));
    }

    /**
     * 表格导出质检员
     */

    @Log(title = "质检员导出数据管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("quality:qualityMan:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgQuality lgQuality)
    {
        List<LgQuality> list = lgQualityService.selectLgQualityList(lgQuality);
        ExcelUtil<LgQuality> util = new ExcelUtil<LgQuality>(LgQuality.class);
        util.exportExcel(response, list, "质检员数据");
    }

    /**
     * 删除质检员
     */
    @RequiresPermissions("quality:qualityMan:remove")
    @Log(title = "质检员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(lgQualityService.deleteLgQualityByIds(ids));
    }

    /**
     * 获取质检员选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<LgQuality> lgQualities = lgQualityService.selectLgQualityAll();
        return success(lgQualities);
    }

}
